package com.chaodroid.flyui.view.viewpager.loop;

import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

/**
 * Created by CHEN on 2017/12/20.
 */

public class LoopPagerAdapter extends PagerAdapter {

    private static final String TAG = "LoopPagerAdapter";

    private PagerAdapter mPagerAdapter;
    private int mItemCount;

    public LoopPagerAdapter(@NonNull PagerAdapter adapter) {
        mPagerAdapter = adapter;
        mItemCount = mPagerAdapter.getCount();
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    final public Object instantiateItem(ViewGroup container, int position) {
        View item = (View) mPagerAdapter.instantiateItem(container, position % mItemCount);
        ViewParent parent = item.getParent();
        if (parent != null) {
            ((ViewGroup)parent).removeView(item);
        }
        container.addView(item);
        return item;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //Warning：不要在这里调用removeView
    }


}
